package com.cloudfunx.common.util;

import com.cloudfunx.common.exception.BizException;
import com.cloudfunx.common.exception.BizRetStatus;
import javafx.beans.binding.ObjectBinding;

import java.util.Arrays;
import java.util.Objects;

/**
 * @author linjiayu
 */
public class AssertKit {

    public static void nonNull(Object... obj) {
        Arrays.stream(obj).forEach(AssertKit::nonNull);
    }

    public static void nonNull(Object obj) {
        nonNull(obj, "参数无法为空");
    }

    public static void nonNull(Object obj, BizRetStatus status) {
        if (obj == null) {
            throwEx(status);
        }
        if (obj instanceof String){
            if ("".equals(obj)){
                throwEx(status);
            }
        }
    }

    public static void nonNull(Object obj, String ex) {
        if (obj == null) {
            throwEx(ex);
        }
        if (obj instanceof String){
            if ("".equals(obj)){
                throwEx(ex);
            }
        }
    }

    public static void isNull(Object... obj) {
        Arrays.stream(obj).forEach(AssertKit::isNull);
    }

    public static void isNull(Object obj) {
        nonNull(obj, "参数无法为空");
    }

    public static void isNull(Object obj, BizRetStatus status) {
        if (obj != null) {
            throwEx(status);
        }
    }

    public static void isNull(Object obj, String ex) {
        if (obj != null) {
            throwEx(ex);
        }
    }

    public static void nonBlank(String str) {
        nonBlank(str, "参数无法为空");
    }

    public static void nonBlank(String str, String ex) {
        if (str == null || "".equals(str)) {
            throwEx(ex);
        }
    }

    public static void nonFalse(boolean flag) {
        nonFalse(flag, "判断错误");
    }

    public static void nonFalse(boolean flag, String ex) {
        if (!flag) {
            throwEx(ex);
        }
    }

    public static void isTrue(boolean flag) {
        isTrue(flag, "判断错误");
    }

    public static void isTrue(boolean flag, String ex) {
        if (!flag) {
            throwEx(ex);
        }
    }

    public static void isTrue(boolean flag, BizRetStatus status) {
        if (!flag) {
            throwEx(status);
        }
    }

    public static void nonTrue(boolean flag) {
        nonTrue(flag, "判断错误");
    }

    public static void nonTrue(boolean flag, String ex) {
        if (flag) {
            throwEx(ex);
        }
    }

    public static void gt0(Integer count) {
        gt0(count,"参数小于等于零");
    }

    public static void gt0(Integer count, String ex) {
        if(count <= 0){
            throwEx(ex);
        }
    }

    public static void gt0(Integer count, BizRetStatus status) {
        if(count <= 0){
            throwEx(status);
        }
    }

    public static void eq(Object first,Object second) {
        eq(first,second,"判断错误");
    }

    public static void eq(Object first,Object second,String ex) {
        if(!Objects.equals(first,second)){
            throwEx(ex);
        }
    }

    public static void le0(Integer count) {
        le0(count,"参数大于零");
    }

    public static void le0(Integer count,String ex) {
        if(count > 0){
            throwEx(ex);
        }
    }

    public static void ge0(int count, String ex) {
        if(count < 0){
            throwEx(ex);
        }
    }
    public static void ge0(int count, BizRetStatus status) {
        if(count < 0){
            throwEx(status);
        }
    }
    public static void ge0(int count) {
        ge0(count,"参数小于零");
    }

    private static void throwEx(String ex) {
        throw new BizException(ex);
    }

    private static void throwEx(BizRetStatus status) {
        throw new BizException(status);
    }

}
